package com.chinda.common.utils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 结果类
 *
 * @author Wang Chinda
 * @date 2020/8/8
 * @see
 * @since 1.0
 */
public class R extends HashMap<String, Object> implements Serializable {
    private static final long serialVersionUID = 1L;

    private static final String DEFAULT_KEY = "data";

    public R() {
        put("code", 0);
        put("msg", "success");
    }

    public static R error() {
        return error(500, "未知异常，请联系管理员");
    }

    public static R error(String msg) {
        return error(500, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        return new R();
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public R set(Object value) {
        super.put(DEFAULT_KEY, value);
        return this;
    }
    public Object get() {
        return super.get(DEFAULT_KEY);
    }
}
